// 380. O(1) 时间插入、删除和获取随机元素
var RandomizedSet = function () {
    this.nums = []
    this.indices = new Map()
};

/** 
 * @param {number} val
 * @return {boolean}
 */
RandomizedSet.prototype.insert = function (val) {
    if (this.indices.has(val)) return false
    let index = this.nums.length
    this.nums.push(val)
    this.indices.set(val, index)
    return true
};

/** 
 * @param {number} val
 * @return {boolean}
 */
/* 让当前值与最后一个值交换，然后删掉最后一个值 */
RandomizedSet.prototype.remove = function (val) {
    if (!this.indices.has(val)) return false
    let id = this.indices.get(val)
    this.nums[id] = this.nums[this.nums.length - 1]
    this.indices.set(this.nums[id], id)
    this.nums.pop();
    this.indices.delete(val)
    return true
};

/**
 * @return {number}
 */
RandomizedSet.prototype.getRandom = function () {
    const randomIndex = Math.floor(Math.random() * this.nums.length)
    return this.nums[randomIndex]
};
var random = new RandomizedSet()
